const express = require('express')
const http = require('http')
const https = require('https')
const app = express()
const port = 3000

console.log('3000')

app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Methods', '*');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next();
});

/*
  musicName
 */
app.get('/kugou', (req, res) => {
  let musicName = req.query.musicName || ''
  let kugou = `http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=${musicName}&page=1&pagesize=20&showtype=1`
  http.get(kugou, rs => {
    let rawData = ''
    rs.on('data', (chunk) => {
      rawData += chunk;
    });
    rs.on('end', () => {
      try {
        const parsedData = JSON.parse(rawData);
        res.send(parsedData)
      } catch (e) {
        console.error(e.message);
      }
    })
  })
})


/*
  FileHash
  AlbumID
 */
app.get('/geturl', (req, res) => {
  let FileHash = req.query.FileHash || ''
  let AlbumID = req.queryAlbumID || ''
  let url = `https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=${FileHash}&album_id=${AlbumID}&dfid=0PZjfr0AVeel0m9ux31B2lQX&mid=24d4f5cf7600fb1fc9c55d5adcfc65f0&platid=4&_=1598086117845`
  https.get(url, rs => {
    let rawData = ''
    rs.on('data', (chunk) => {
      rawData += chunk;
    });
    rs.on('end', () => {
      try {
        const parsedData = JSON.parse(rawData);
        res.send(parsedData)
      } catch (e) {
        console.error(e.message);
      }
    })
  })
})


app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})